package com.ruoyi.web.controller.wx;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ByOrder;
import com.ruoyi.system.service.IByOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/wx/order")
@Validated
public class WxOderController {
    @Autowired
    private IByOrderService orderService;

    @PostMapping("getBsEvalById")
    public AjaxResult getBsEvalById(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        List<JSONObject> list = orderService.getBsEvalById(json);
        return AjaxResult.success(list);
    }


    @PostMapping("wxPayOrderMM")
    public AjaxResult wxPayOrderMM(@RequestBody String param, HttpServletRequest request){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = orderService.payPayOrder(json,request);
        return AjaxResult.success(obj);
    }

    /**
     * 进行开启或者停止计时的任务
     * @param param
     * @return
     */
    @PostMapping("orderDetailById")
    public AjaxResult orderDetailById(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = orderService.orderDetailById(json);
        return AjaxResult.success(obj);
    }

    /**
     * 进行开启或者停止计时的任务
     * @param param
     * @return
     */
    @PostMapping("orderTimeMananger")
    public AjaxResult orderTimeMananger(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = orderService.orderTimeMananger(json);
        return AjaxResult.success(obj);
    }

    /**
     * 判断开始或结束计时
     * @return
     */
    @PostMapping("isStartOrEnd")
    public AjaxResult isStartOrEnd(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String orderid = json.getString("orderid");
        String status = orderService.isStartOrEnd(orderid);
        if ("3".equals(status)){
            JSONObject start = new JSONObject();
            start.put("result","开始");
            return AjaxResult.success(start);
        }else if ("4".equals(status)){
            JSONObject end = new JSONObject();
            end.put("result","结束");
            return AjaxResult.success(end);
        }else {
            return AjaxResult.error("当前状态暂不支持~");
        }
    }

    @PostMapping("saveOrderEval")
    public Object saveOrderEval(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = orderService.saveOrderEval(json);
        return AjaxResult.success(obj);
    }


    @PostMapping("pay-notify")
    public Object payNotify(HttpServletRequest request, HttpServletResponse response) {
        return orderService.payNotify(request, response);
    }


    @PostMapping("payPayOrder")
    public Object payPayOrder(@RequestBody String param, HttpServletRequest request){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = orderService.payPayOrder(json, request);
        return AjaxResult.success(obj);
    }


    @PostMapping("payDjOrder")
    public Object payDjOrder(@RequestBody String param, HttpServletRequest request){
        JSONObject json = JSONObject.parseObject(param);
        Object obj = orderService.prepay(json, request);
        return AjaxResult.success(obj);
    }


    @PostMapping("quitOrder")
    public Object quitOrder(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        orderService.quitOrder(json);
        return AjaxResult.success();
    }

    @PostMapping("saveEntity")
    public Object saveEntity(@RequestBody String param, HttpServletRequest request){
        JSONObject json = JSONObject.parseObject(param);
        orderService.savaJzWxOrder(json);// 进行保存订单信息
        // 进行获取订单信息
        String wxid = json.getString("wxid");
        ByOrder entity = orderService.getOrderByWxId(wxid);
        if(StringUtils.isEmpty(entity.getBsId())){
            return AjaxResult.error("400","获取订单错误");
        }
        json.put("id",entity.getId());

        Object obj = orderService.prepay(json, request);
        return AjaxResult.success(obj);
    }


    @PostMapping("getOrderByWxid")
    public Object getOrderByWxid(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String code = json.getString("code");
        if(StringUtils.isEmpty(code)){
            return AjaxResult.success();
        }
        List<JSONObject> list = orderService.getOrderByWxid(json);

        return AjaxResult.success(list);
    }


    @PostMapping("startOrder")
    public Object startOrder(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        orderService.startOrder(json);
        return AjaxResult.success();
    }

    @PostMapping("stopOrder")
    public Object stopOrder(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        orderService.stopOrder(json);
        return AjaxResult.success();
    }

    @PostMapping("delDjOrder")
    public Object delDjOrder(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        Long ids = json.getLong("id");
        orderService.deleteByOrderById(ids);

        return AjaxResult.success();
    }

    /**
     * 选中优惠券时根据userid和订单号将应付尾款减去优惠券的金额
     * @param param
     * @return
     */
    @PostMapping("updateMoney")
    public Object updateMoney(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String wxid = json.getString("wxid");
        String orderId = json.getString("orderId");
        String yhqId = json.getString("yhqId");
        int result = orderService.updateMoney(wxid, yhqId, orderId);
        if (result == 0){
            return AjaxResult.error();
        }
        return AjaxResult.success(result);
    }


    /**
     * 取消选中优惠券时把优惠券的选中状态改成0，还原尾款
     * @param param
     * @return
     */
    @PostMapping("updateYhqStatus")
    public Object updateYhqStatus(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String yhqId = json.getString("yhqid");
        String orderId = json.getString("orderid");
        int result = orderService.updateYhqStatus(yhqId,orderId);
        if (result == 0){
            return AjaxResult.error();
        }
        return AjaxResult.success(result);
    }

    /**
     * 支付成功将优惠券状态改为已使用，订单状态改成完成
     * @param param
     * @return
     */
    @PostMapping("successPay")
    public Object successPay(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String yhqId = json.getString("yhqid");
        String orderId = json.getString("orderid");
        String userid = json.getString("wxid");
        int result = orderService.successPay(yhqId,orderId,userid);
        if (result == 0){
            return AjaxResult.error();
        }
        return AjaxResult.success(result);
    }
}
